// pages/login/login.ts
import { IAppOption } from "../../../typings";
import { userProfileAPI } from "../../api/index";
import { loginAPI } from "../../api/open"
import { zcc } from "../../utils/util";
// import { loginoutAPI } from "../../api/index"
Page({
  loginHandle: async function () {
    if (!this.data.isCheckPrivacy) return wx.showToast({
      title: "请先同意《用户协议》《隐私政策》",
      duration: 2000,
      icon: "none"
    });
    let para = {
      tenantName: this.data.tenantName,
      userName: this.data.userName,
      password: this.data.password
    }
    const res: any = await loginAPI(para);
    console.log(res, "res login")
    if (res.code === '000') {
      this.getUserProfile()
      wx.setStorageSync('cacheLoginInfo', para)
    }
  },
  getUserProfile: async function () {
    zcc.getUserProfile({
      loading: true,
      onSuccess: () => {
        setTimeout(() => {
          wx.reLaunch({
            url: "/pages/index/index"
          })
        }, 1000);
      }
    })
  },

  suffixIconClick: function (e: any) {
    if (e.detail.trigger === "suffix-icon") {
      var temp = !this.data.passwordFlag;
      this.setData({
        passwordFlag: temp
      })
    }
  },
  inputChange: function (e: any) {
    let id = e.currentTarget.dataset.id;
    let key = "status" + id;

    this.setData({
      password: this.data.password
    })

    let disableStatus = this.getDisabled();
    if (e.detail.value && e.detail.value.length > 0) {
      this.setData({ [key]: '', disableStatus: disableStatus })
    } else {
      this.setData({ [key]: 'error', disableStatus: disableStatus })
    }
  },
  clearHandle: function (e: any) {
    let id = e.currentTarget.dataset.id;
    let key = "status" + id;
    let disableStatus = this.getDisabled();
    this.setData({ [key]: 'error', disableStatus: disableStatus })
  },
  getDisabled: function () {
    if (this.data.tenantName && this.data.userName && this.data.password) {
      return false;
    } else {
      return true;
    }
  },
  init: function () {
    const cache = wx.getStorageSync('cacheLoginInfo')
    console.log('cache user = ' + JSON.stringify(cache))
    this.setData({
      tenantName: cache.tenantName || '',
      userName: cache.userName || '',
      password: cache.password || '',
    })
    let disableStatus = this.getDisabled();
    this.setData({
      disableStatus: disableStatus,
    })

  },
  /**
   * 页面的初始数据
   */
  data: {

    tenantName: "",
    userName: "",
    password: "",
    inputStyle: "border: 1px solid #D0D3D5;border-radius:6px;height: 16px;font-size: 28rpx;position:relative;",
    placeholderStyle: "color: #86909C;font-size: 28rpx;",
    passwordFlag: true,
    status1: "",
    status2: "",
    status3: "",
    disableStatus: true,
    safeBottom: 0,
    isCheckPrivacy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

    const app = getApp();
    this.setData({
      safeBottom: app.globalData.screenHeight - app.globalData.safeArea.bottom
    })
  },
  goWebpage(id: string, title: string) {
    const weburl = "https://admin2.bcyhl.com/docpage?id=" + id;
    zcc.goWebpage(weburl, title)
  },
  //用户协议、隐私政策
  handleNavigate(e: any) {
    const type = e.currentTarget.dataset.type;
    switch (type) {
      case 'agreement':
        // 用户协议
        this.goWebpage('17', '用户协议')
        break;
      case 'privacy':
        // 隐私政策
        this.goWebpage('18', '隐私政策')
        break;
    }
  },

  checkPrivacy() {
    this.setData({
      isCheckPrivacy: !this.data.isCheckPrivacy
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})